package me.zhengjie.modules.other.rest;

import com.qiaofang.applet.service.other.service.CarouselService;
import com.qiaofang.applet.service.other.service.CityCompanyService;
import com.qiaofang.applet.service.other.service.dto.CarouselDetailDto;
import com.qiaofang.applet.service.other.service.dto.CarouselQueryDto;
import com.qiaofang.applet.service.other.service.mapstruct.CarouselMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.service.UserService;
import me.zhengjie.utils.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by IDEA.
 *
 * @author QF2019
 * @company qiaofang
 * @date 2020/9/12 11:35
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "内容：轮播图管理")
@RequestMapping("/api/carousel")
public class CarouselController {

    @Autowired
    private CarouselService carouselService;

    @Autowired
    private CarouselMapper carouselMapper;

    @Autowired
    private CityCompanyService cityCompanyService;

    @Autowired
    private UserService userService;

    @GetMapping
    @ApiOperation(value = "查询轮播图列表", notes = "查询轮播图列表")
    public ResponseEntity<Page<CarouselDetailDto>> list(CarouselQueryDto carouselQueryDto, Pageable pageable) {
        String corporationCode = SecurityUtils.getCorporationCode();
        carouselQueryDto.setCorporationCode(corporationCode);
        carouselQueryDto.setCityCodes(userService.getCityCodeList(SecurityUtils.getCurrentUsername()));
        Page<CarouselDetailDto> page = carouselService.listDetail(carouselQueryDto, pageable);
        initCityName(corporationCode, page.getContent());
        return ResponseEntity.ok(page);
    }

    @PostMapping
    @ApiOperation(value = "添加轮播图详情", notes = "添加轮播图详情")
    public ResponseEntity<Object> create(@RequestBody CarouselDetailDto carouselDetailDto) {
        String corporationCode = SecurityUtils.getCorporationCode();
        carouselDetailDto.setCorporationCode(corporationCode);
        carouselService.saveDetail(carouselMapper.detailDtoToDomain(carouselDetailDto));
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PutMapping
    @ApiOperation(value = "更新轮播图详情", notes = "更新轮播图详情")
    public ResponseEntity<Object> update(@RequestBody CarouselDetailDto carouselDetailDto) {
        carouselService.saveDetail(carouselMapper.detailDtoToDomain(carouselDetailDto));
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @DeleteMapping
    @ApiOperation(value = "删除轮播图", notes = "删除轮播图")
    public ResponseEntity<Object> remove(@RequestBody List<String> carouselIds) {
        if (!CollectionUtils.isEmpty(carouselIds)) {
            carouselService.remove(carouselIds.get(0));
        }
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 初始化城市名称
     * @param corporationCode
     * @param carouselDetailDtos
     */
    private void initCityName(String corporationCode, List<CarouselDetailDto> carouselDetailDtos) {
        Map<String, String> cityMap = cityCompanyService.getCityMap(corporationCode);
        if(CollectionUtils.isEmpty(cityMap)){
            return;
        }

        StringBuilder cityNames;
        for (CarouselDetailDto carouselDetailDto : carouselDetailDtos) {
            cityNames = new StringBuilder();
            for (String cityCode : carouselDetailDto.getCityCodes()) {
                cityNames.append(StringUtils.defaultString(cityMap.get(cityCode))).append(",");
            }

            int len = cityNames.length();
            if(len > 0) {
                carouselDetailDto.setCityNames(len > 0 ? cityNames.substring(0, len - 1) : "");
            }
        }
    }
}